<!-- 搜索项目 -->
<template>
  <h-select-input-popup
    ref="hSelectInputPopup"
    v-model="val"
    v-bind="$attrs"
    v-on="$listeners"
    :params="queryParams"
    :keywords.sync="keywords"
    :getList="urlList"
		:disabled="disabled"
    @getDetail="getDetail"
		searchPlaceholder="请输入项目名称"
    @change="$emit('change', $event)"
		:isMulti="muitFlag"
  >
    <template v-slot:default="{ item }">
      <view class="project-item">
        <view class="option">
          <view class="lf">项目名称</view>
          <view class="rl">{{ item.projectName }}</view>
        </view>
        <!-- <view class="option">
          <view class="lf">签订单位</view>
          <view class="rl">{{ item.signUnit_dictText }}</view>
        </view> -->
        <view class="option">
          <view class="lf">经营团队</view>
          <view class="rl">{{ item.teamName_dictText }}</view>
        </view>
        <view class="option">
          <view class="lf">项目归属年度</view>
          <view class="rl">{{ item.year_dictText }}</view>
        </view>
        <view class="option">
          <view class="lf">业主单位</view>
          <view class="rl">{{ item.ownerUnit_dictText }}</view>
        </view>
        <view class="option">
          <view class="lf">联系人</view>
          <view class="rl">{{ item.contacts_dictText }}</view>
        </view>
        <view class="option">
          <view class="lf">联系方式</view>
          <view class="rl">{{ item.contactTel }}</view>
        </view>
        <view class="option">
          <view class="lf">项目承接人</view>
          <view class="rl">{{ item.undertaker_dictText }}</view>
        </view>
        <view class="option">
          <view class="lf">所在地址</view>
          <view class="rl">{{ getAddress(item.areaCode) }}</view>
        </view>
        <view class="option">
          <view class="lf">项目阶段</view>
          <view class="rl">{{ item.status_dictText }}</view>
        </view>
      </view>
    </template>
  </h-select-input-popup>
</template>

<script>
import { queryOmcPrepareProjectList, queryOmcProjectList } from '@/api/index.js'
import { erpOmcProjectId, erpOmcProjectIds } from '@/api/index.js'
import address from '@/utils/address.js'
export default {
  name: 'HSelectProjectInputPopup',
  data() {
    return {
      address,
      params: {
        // order: 'desc',
        pageNo: 1,
        pageSize: 10,
      },
      keywords: '',
    }
  },
  model: {
    prop: 'value',
    event: 'input',
  },
  props: {
    value: {
      type: [String, Number, Array],
      default: null,
    },
    signUnit: {
      type: String,
      default: '',
    },
		// 公司id
		companyId: {
			type: String,
			default: '',
		},
    teamName2: {
      type: String,
      default: '',
    },
    projectStatus: {
      type: [String, Number, Array, Boolean],
      default: '',
    },
    api: {
      type: Function,
      default: queryOmcPrepareProjectList,
    },
		
		useType: {
			type: String,
			required: false,
			default: ''
		},
		dataIsolation: {
			type: String,
			required: false,
			default: ''
		},
		dataType: {
			type: String,
			required: false,
			default: ''
		},
		disabled: {
			type: Boolean,
			required: false,
			default: false,
		},
		proDisabled: {
			type: Boolean,
			required: false,
			default: false,
		},
		open: {
			type: Boolean,
			required: false,
			default: false,
		},
		changeStatus: {
			type: String,
			default: ''
		},
		muitFlag: {
      type: Boolean,
      default: false,
		},
		props: {
			type: Object,
			default: null,
		},
		isContractManager: {
			type: Boolean,
			default: false
		},
		// 是否是环评项目
		isBidding: {
			type: String,
			default: null
		},
		// 费用类型
		costType: {
			type: String,
			default: null
		},
		// 投标状态
		tenderStatus: {
			type: String,
			default: null
		},
		// 所属团队
		teamId: {
			type: String,
			default: null
		},
		
		// 项目承接人
		undertaker: {
			type: String,
			default: null
		},
		// 查询条件中团队字段名改为经营/生产团队
		queryParamType: {
			type: String,
			default: null
		},
		// 老系统项目录入标识
		isOldSystem: {
			type: String,
			default: ''
		},
  },
  computed: {
    val: {
      get() {
        return this.value
      },
      set() {
        this.$emit('input', this.value)
      },
    },
		urlList () {
			var tmpUrl = queryOmcPrepareProjectList
			if (this.useType === 'tender') {
				if (this.status == '2') {
					tmpUrl = queryOmcProjectList
				}
			} else {
				if (this.status == '1') {
					tmpUrl = queryOmcPrepareProjectList
				} else{
					tmpUrl = queryOmcProjectList
				}
			}
			return tmpUrl
		},
    queryParams() {
			
      const result = {
        ...this.params,
      }
      if (this.projectStatus) {
        result.status = this.projectStatus
      }
      if (this.dataIsolation) {
        result.dataIsolation = this.dataIsolation
      }
      if (this.dataType) {
        result.dataType = this.dataType
      }
      if (this.signUnit) {
        result.signUnit = this.signUnit
      }
			if (this.companyId) {
        result.companyId = this.companyId
      }
			if (this.teamId) {
        result.teamName2 = this.teamId
      }
			if (this.costType) {
        result.costType = this.costType
      }
      if (this.keywords) {
        result.projectName = '*' + this.keywords + '*'
      }
			
			if (this.changeStatus) {
				result.changeStatus = this.changeStatus
			}
			if (this.tenderStatus) {
				result.projectTenderStatus = this.tenderStatus
			}
			if(this.isBidding) {
				result.isBidding = this.isBidding
			}
			if(this.undertaker) {
				result.undertaker = this.undertaker
			}
			
			if(this.queryParamType=='2'){
				result.isManagement = true
			}
			if(this.isOldSystem){
				result.isOldSystem = this.isOldSystem
			}
			
			result.dataIsolation = this.dataIsolation
			result.dataType = this.dataType
      return result
    },
  },
  methods: {
    // 获取详情
    // getDetail(value) {
    //   erpOmcProjectId({ id: value }).then((res) => {
    //     if (res.success) {
    //       this.$refs.hSelectInputPopup.title = res.result.projectName
    //       this.$emit('select', {
    //         data: res.result,
    //         id: value,
    //       })
    //     }
    //   })
    // },
		getDetail(value) {
		  if (this.muitFlag || value.split(',').length>0) {
		    erpOmcProjectIds({ ids: value }).then((res) => {
		      if (res.success) {
		        this.$refs.hSelectInputPopup.title = res.result.map((i) => i.projectName).join(',')
		        this.$emit('select', {
		          data: res.result,
		          id: value,
		        })
		      }
		    })
		    return
		  } else {
		    erpOmcProjectId({ id: value }).then((res) => {
		      if (res.success) {
		        this.$refs.hSelectInputPopup.title = res.result.projectName
		        this.$emit('select', {
		          data: res.result,
		          id: value,
		        })
		      }
		    })
		  }
		},

    // 地址显示
    getAddress(areaCode) {
      var arr = areaCode.split(',')
      var addressDetail = ''
      this.address.map((value) => {
        if (arr[0] && value.code == arr[0]) {
          addressDetail = value.name
          value.children.map((val) => {
            if (arr[1] && val.code == arr[1]) {
              addressDetail = addressDetail + val.name
              val.children.map((item) => {
                if (arr[2] && item.code == arr[2]) {
                  addressDetail = addressDetail + item.name
                }
              })
            }
          })
        }
      })
      return addressDetail
    },
  },
}
</script>

<style lang="scss" scoped>
// 按钮
.btn {
  position: fixed;
  bottom: 20rpx;
  width: 95%;
}
</style>
